﻿using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class SpellSlot : MonoBehaviour 
{
	public Spell spell;
	public Image spriteSlot;
	public Sprite emptySprite;
	public Image cooldownImage;
	[HideInInspector]public SpellManager spellManager;
	[HideInInspector] public int spellIndex;
	public Color textColor;
	public Color textDisabledColor;
	public Text letter;
	void Awake()
	{
		cooldownImage.type = Image.Type.Filled;
	}

	public void SetTo(Spell inputSpell)
	{
		if (inputSpell!=null)
		{
			spell = inputSpell;
			spriteSlot.sprite = spell.icon;
			spellIndex = (int)System.Enum.Parse(typeof(SpellManager.spellType),spell.spellName);
		}else
		{
			spell = null;
			spriteSlot.sprite = emptySprite;
		}
	}

	void Update()
	{
		if (spell == null)
		{
			if (letter!=null)
			{
				letter.color = textDisabledColor;
				cooldownImage.fillAmount = 1;
			}
		} else
		{	
			float remainingTime = spellManager.nextCastTimes[spellIndex] - Time.time;
			if (remainingTime > 0)
			{
				cooldownImage.fillAmount = Mathf.Clamp(remainingTime/spellManager.coolDownTimes[spellIndex],0,1);
				if (letter!=null)
				{
					letter.color = textDisabledColor;
				}
			} else
			{
				
					cooldownImage.fillAmount = 0;
				if (letter!=null)
				{
					letter.color = textColor;
				}
				
			}
		}
	}

}
